import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;

import javax.swing.border.*;
import javax.swing.*;

public final class SplashScreen extends JWindow implements ActionListener
{

private static final long serialVersionUID = 1L;

 private JFrame _frame;
 private String _imageFile;

   public SplashScreen(String imageFile)
   {

       Toolkit toolkit;
       Dimension scr;
       int scrX;
       int scrY;
       _imageFile = imageFile;
       
       _frame = new JFrame(); 
       _frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

       showSplashScreen(_frame.getContentPane());     
       
       _frame.pack();    
       _frame.setVisible(true);
       _frame.setResizable(false);
       
       toolkit = _frame.getToolkit(); 
       scr = toolkit.getScreenSize();
       
       scrX  = (int) (scr.getWidth()  - getWidth()) / 2;
       scrY  = (int) (scr.getHeight() - getHeight()) / 2;

       _frame.setLocation(scrX, scrY); 
       
   }

   private void showSplashScreen(Container pane) 
   {
       ImageIcon image   = new ImageIcon( getClass().getResource(_imageFile));
       
       JLabel label = new JLabel(image);

       pane.add(label);
       
      JButton closeButton = new JButton();

      JLabel    lblBack = new JLabel( image );

      lblBack.setBounds( 0, 0, image.getIconWidth(), image.getIconHeight() );
      getLayeredPane().add( lblBack, new Integer( Integer.MIN_VALUE ) );

      setSize( image.getIconWidth(), image.getIconHeight() );
      
      closeButton = new JButton("Start");
      closeButton.addActionListener(this);
      closeButton.setVerticalTextPosition(AbstractButton.BOTTOM);
      closeButton.setHorizontalTextPosition(AbstractButton.CENTER);
      pane.add(closeButton, BorderLayout.PAGE_END);

   }
   
   
    public void  actionPerformed(ActionEvent e) 
    {
        final int ERROR = -1;
        
        _frame.setVisible(false);
        
        try
        {
            KaonaGame.start();
        
        } catch (FileNotFoundException e1)
        {
            System.exit(ERROR);
        }
    }
    
}
